# -*- coding: utf-8 -*-
import getpass
import os

# 项目根目录
import smtplib
import time
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

project_path = "/Users/hiking/Desktop/GULAILECode/ios"
# build mode
configuration = "Release"
scheme = "GuLaiLe"
archivePath = "/Users/hiking/Desktop/test"
archiveFile = "/Users/hiking/Desktop/test/test.xcarchive"
exportOptionsPlist = "/Users/hiking/Desktop/success/ExportOptions.plist"
exportPath = "/Users/hiking/Desktop/success"

# 发送邮件配置QQ：smtp.qq.com 端口号465   阿里企业：smtp.mxhichina.com 端口号465
SEND_MAIL = "haij@qianlong2.net"
SMTP_SERVER = "office.qianlong.com.cn"  # smtp邮箱域名链接
SMTP_SERVER_PORT = "25"  # smtp邮箱域名链接端口号
ACCEPT_MAILS = ['haij@qianlong2.net', 'gouj@qianlong2.net', 'liuqy@qianlong2.net',
                'liuyc@qianlong2.net', 'zhousc@qianlong2.net', 'raozy@qianlong2.net',
                'liusc@qianlong2.net', 'lirw@qianlong2.net', 'jianyb@qianlong2.net',
                'luojx@qianlong2.net', 'yanghao@qianlong2.net', 'dengdj@qianlong2.net',
                'wangzy@qianlong2.net', 'linh@qianlong2.net', 'luod@qianlong2.net', 'zhanglp@qianlong2.net',
                'chenqy@qianlong2.net', 'shup@qianlong2.net', 'wangmx@qianlong2.net']
UPDATE_CONTENT = "更新上传内容配置"  # 更新上传内容配置
smtp_auth_password = "8888"


def upload_svn():
    SVN_ADDRESS = "http://hj@152.136.40.228:8080/svn/qqbipa/期权资管/GuLaiLe"
    # os.system('svn delete  %s%s -m "%s"' % (SVN_ADDRESS, ipa_filename, "delete updata"))
    os.system('svn --username=hj  import -m %s  %s %s/%s' % (
        "1", "/Users/hiking/Desktop/success/GuLaiLe.ipa", SVN_ADDRESS, "GuLaiLe.ipa"))


# 打包ipa
# Clean Project
def clean_project():
    print("\033[1;35m==========>> Start clean! <<==========\033[0m")
    os.chdir(project_path)
    # xcodebuild clean -scheme GuLaiLe -configuration Release
    os.system('xcodebuild clean -scheme %s -configuration %s || exit ' % (scheme, configuration))
    print("\033[1;32m==========>> Clean success! <<==========\033[0m")


# Build Project
def build_project():
    print("\033[1;35m==========>> Start archive! <<==========\033[0m")
    os.chdir(project_path)
    # xcodebuild archive - scheme GuLaiLe - configuration Release - archivePath /Users/hiking/Desktop/test
    os.system(
        'xcodebuild archive  -scheme %s -configuration %s -archivePath %s '
        'PROVISIONING_PROFILE_VALUE=\'Automatic\' || exit' % (scheme, configuration, archivePath))
    print("\033[1;32m==========>> Archive success! <<==========\033[0m")


# 导出ipa应用包
def exportArchive():
    print("\033[1;35m==========>> Start export ipa! <<==========\033[0m")
    # xcodebuild - exportArchive - archivePath / Users / hiking / Desktop / test / test.xcarchive - exportPath / Users / hiking / Desktop / success / Tyrion.ipa
    os.system("xcodebuild -exportArchive -archivePath %s -exportOptionsPlist %s -exportPath %s || exit"
              % (archiveFile, exportOptionsPlist, exportPath))
    print("\033[1;32m=======>> Export ipa success! <<========\033[0m")


# 输入邮箱密码
def input_mail_password():
    global smtp_auth_password
    smtp_auth_password = "8888"  # getpass.getpass('password: ')
    server = smtplib.SMTP(SMTP_SERVER, SMTP_SERVER_PORT)
    server.login(SEND_MAIL, smtp_auth_password)


content = """\
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>课程表</title>
</head>
<body>
 <h3>Hello!大家好!<br>乾隆自营版本iOS正式包已提交SVN</h3>
<table border="1" cellspacing="0" width="100%">

    <tr align="center">
        <td bgcolor="#e9faff" colspan="1">提交版本</td>
        <td colspan="3">V5.81.43(20190815-1)</td>
        <td bgcolor="#e9faff" colspan="1">券商代码</td>
        <td colspan="3">0</td>
    </tr>
    <tr align="center">
        <td bgcolor="#e9faff" colspan="1">提交日期</td>
        <td colspan="3">2019/08/15</td>
        <td bgcolor="#e9faff" colspan="1">bundleId</td>
        <td colspan="3">com.qianlong.zy</td>
    </tr>
    <tr align="center">
        <td bgcolor="#e9faff" colspan="1">打包名称</td>
        <td colspan="3">QLGP_V5.81.43(20190815-1zs).ipa</td>
        <td bgcolor="#e9faff" colspan="1">项目描述</td>
        <td colspan="3">自营iOS</td>
    </tr>
    <tr align="center">
        <td bgcolor="#e9faff" colspan="1">环境选择</td>
        <td colspan="3">正式环境</td>
        <td bgcolor="#e9faff" colspan="1">SVNRevison</td>
        <td colspan="3">1079</td>
    </tr>
    <tr align="center">
        <td bgcolor="#e9faff" colspan="1">认证服务器地址</td>
        <td colspan="3">ip1=newhq1.ql18.mobi:20005
            ip2=114.80.108.149:20005
            ip3=newhq2.ql18.mobi:20005
            ip4=42.51.11.8:20005
        </td>
        <td bgcolor="#e9faff" colspan="1">提交包文件地址</td>
        <td colspan="3">http://152.136.40.228:8080/svn/stockipa/</td>
    </tr>

    <tr align="center" bgcolor="#e9faff">
        <td colspan="1">编号(TAPD)</td>
        <td colspan="2">问 题 描 述</td>
        <td colspan="1">等 级</td>
        <td colspan="2">发生原因 和 修改方案,影响</td>
    </tr>

    <tr align="center">
        <td colspan="1">1</td>
        <td colspan="2">【iphone_bug股票模拟-历史委托，未取到时间，导致委托时间显示有问题】</td>
        <td colspan="1">3-高</td>
        <td colspan="2">发生原因：代码未兼容<br/>
            修改方案：针对委托时间字段未获取到时,根据其他字段配置<br/>
            影响范围：历史委托界面
        </td>

    </tr>
    <tr align="center">
        <td colspan="1">2</td>
        <td colspan="2">【iphone_bug安装APP后，不手动在股票交易页面设置默认券商。直接去闪电下单页面点击买卖等按钮无反应】</td>
        <td colspan="1">3-高</td>
        <td colspan="2">发生原因：代码原因<br/>
            修改方案：处理券商请求,设置股票券商的第一个为默认券商<br/>
            影响范围：交易模块
        </td>

    </tr>


    <tr align="center" bgcolor="#e9faff">
        <td colspan="1">编号(TAPD)</td>
        <td colspan="2">需 求 描 述</td>
        <td colspan="1">等 级</td>
        <td colspan="2">修改方案,影响范围</td>
    </tr>
     <tr align="center">
        <td colspan="1">1</td>
        <td colspan="2">【iphone_bug【需求】引导页、功能指引修改】</td>
        <td colspan="1">3-高</td>
        <td colspan="2">
            修改方案：按需求完成<br/>
            影响范围：启动页面
        </td>

    </tr>

</table>
<h3>自测结果:通过 签名:haij海境</h3>
 <h3 bgcolor="red">备注:这是一封自动化脚本邮件,暂不支持邮件回复,该邮件在iOS脚本打包并自动提交SVN后,自动发送。模板为html如有错误，望指正修改!Thx!</h3>
<hr>
海境</br>
成都乾隆科技有限公司|开发部|手机组
</body>
</html>


"""


# 发邮件,附带png安装二维码，这里是fir生成的，SVN渠道暂不支持图片
def send_mail_method():
    msg = MIMEMultipart()

    # html格式
    html = content
    htm = MIMEText(html, 'html', 'utf-8')
    msg.attach(htm)

    # msgaaa = MIMEText(UPDATE_CONTENT, 'plain', 'utf-8')
    # msg.attach(msgaaa)

    msg['From'] = SEND_MAIL
    msg['To'] = ",".join(ACCEPT_MAILS)
    msg['Subject'] = Header(
        "乾隆自营 iOS_正式 V5.81.43(20190815-1),更新时间(脚本生成时间)：" + time.strftime('%Y年%m月%d日%H:%M:%S',
                                                                         time.localtime(time.time())),
        'utf-8').encode()
    # print "%s msg配置信息" % (msg)
    server = smtplib.SMTP(SMTP_SERVER, SMTP_SERVER_PORT)
    server.login(SEND_MAIL, smtp_auth_password)
    server.sendmail(SEND_MAIL, ACCEPT_MAILS, msg.as_string())
    server.quit()


if __name__ == '__main__':
    # step1:打包
    # clean_project()
    # build_project()
    # exportArchive()
    # step2:上传SVN
    # upload_svn()
    # step3:发送邮件
    # send_mail_method()
